﻿namespace SmartSoft.DAL
{
    using System;
    using System.Data;
    using System.Data.SqlClient;using NLog;

    public class DmDtDataAccess
    {
        private static Logger log = LogManager.GetCurrentClassLogger();
        public DataSet GetDataSet()
        {
            DataSet set2;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataSet dataSet = new DataSet("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmDt_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                adapter.SelectCommand = command;
                adapter.Fill(dataSet, "ITC");
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return set2;
        }

        public DataSet GetDataSet(string ps_Code)
        {
            DataSet set2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dt", ps_Code) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataSet dataSet = new DataSet("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmDt_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataSet, "ITC");
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return set2;
        }

        //public DataSet GetDataSetByParent(string ps_ParentCode)
        //{
        //    DataSet set2;
        //    SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Nh_Dt", ps_ParentCode) };
        //    SqlConnection SSConnection = SSDataAccess.SSConnection;
        //    DataSet dataSet = new DataSet("VBSP");
        //    SqlDataAdapter adapter = new SqlDataAdapter();
        //    SqlCommand command = new SqlCommand();
        //    try
        //    {
        //        command.CommandText = "DmDt_Get";
        //        command.CommandType = CommandType.StoredProcedure;
        //        command.Connection = SSConnection;
        //        command.Parameters.AddRange(values);
        //        adapter.SelectCommand = command;
        //        adapter.Fill(dataSet, "ITC");
        //        set2 = dataSet;
        //    }
        //    catch (Exception exception)
        //    {
        //        log.ErrorException(string.Empty, exception); 
        //        throw new NotSupportedException("Invalid expression", exception);
        //    }
        //    finally
        //    {
        //        SSDataAccess.Close(SSConnection);
        //    }
        //    return set2;
        //}

        public DataSet GetDataSetByParent(string ps_ParentCode, string ps_Ma_Dvcs)
        {
            DataSet set2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Nh_Dt", ps_ParentCode), new SqlParameter("p_Ma_Dvcs", ps_Ma_Dvcs) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataSet dataSet = new DataSet("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmDt_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataSet, "ITC");
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception);
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return set2;
        }

        public int SaveRow(bool pb_NewChecked, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { 
                new SqlParameter("p_Moi_Sua", pb_NewChecked), new SqlParameter("p_Stt", row["Stt"]), new SqlParameter("p_Ma_Dt", row["Ma_Dt"]), new SqlParameter("p_Ten_Dt", row["Ten_Dt"]), new SqlParameter("p_Ten_Dt_E", row["Ten_Dt_E"]), new SqlParameter("p_Dia_Chi", row["Dia_Chi"]), new SqlParameter("p_Dia_Chi_E", row["Dia_Chi_E"]), new SqlParameter("p_Ms_Thue", row["Ms_Thue"]), new SqlParameter("p_Doi_Tac", row["Doi_Tac"]), new SqlParameter("p_Doi_Tac_E", row["Doi_Tac_E"]), new SqlParameter("p_Website", row["Website"]), new SqlParameter("p_Email", row["Email"]), new SqlParameter("p_Phones", row["Phones"]), new SqlParameter("p_Faxes", row["Faxes"]), new SqlParameter("p_Tk_NH", row["Tk_NH"]), new SqlParameter("p_Chu_Tk", row["Chu_Tk"]), 
                new SqlParameter("p_Ten_NH", row["Ten_NH"]), new SqlParameter("p_Ma_Nh_Dt", row["Ma_Nh_Dt"]), new SqlParameter("p_Stt_Nh_Dt", row["Stt_Nh_Dt"]), new SqlParameter("p_Is_Stop", row["Is_Stop"]), new SqlParameter("p_No_Co", row["No_Co"]), new SqlParameter("p_Ma_Nv", row["Ma_Nv"]), new SqlParameter("p_Tien_Lmt", row["Tien_Lmt"]), new SqlParameter("p_Han_Tt", row["Han_Tt"]), new SqlParameter("p_Ma_UT", row["Ma_UT"]), new SqlParameter("p_Ma_Loai1", row["Ma_Loai1"]), new SqlParameter("p_Ma_Loai2", row["Ma_Loai2"]), new SqlParameter("p_Ma_Loai3", row["Ma_Loai3"]), new SqlParameter("p_Ma_Cv", row["Ma_Cv"]), new SqlParameter("p_Ma_Dvcs", row["Ma_Dvcs"]), new SqlParameter("p_Result", SqlDbType.BigInt)
             };
            values[1].Direction = ParameterDirection.InputOutput;
            values[1].SqlDbType = SqlDbType.BigInt;
            values[30].Direction = ParameterDirection.Output;
            values[30].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmDt_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                row["Stt"] = values[1].Value;
                num = (int) values[30].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return num;
        }
    }
}

